package com.nopay.cdxphone.ui.ativity;

import android.Manifest;
import android.app.Dialog;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nopay.cdxphone.base.BaseActivity;
import com.nopay.cdxphone.custom_view.BottomSheetDialog;
import com.nopay.cdxphone.dialog.DialogCallTimeOut;
import com.nopay.cdxphone.dialog.DialogSettingSim;
import com.nopay.cdxphone.utils.CallTransferUtil;

import java.util.ArrayList;
import java.util.List;

import com.nopay.cdxphone.Constants;
import com.nopay.cdxphone.R;
import com.nopay.cdxphone.databinding.ActivityCallSettingForDoubleSimBinding;

/**
 * @ProjectName: myVisualPhone
 * @Package: cn.cn.myvisualphone.ui.ativity
 * @ClassName: ForgetActivity
 * @Description: 更新用户姓名
 * @Author: anqi
 * @CreateDate: 2021/5/23 8:05
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/23 8:05
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CallSettingForDoubleSIMActivity extends BaseActivity<ActivityCallSettingForDoubleSimBinding> {

    private static final String[] CALL_SIM_LIST = {"单SIM卡外呼-SIM卡1", "单SIM卡外呼-SIM卡2"};

    private int simIndex = 0;
    private boolean enabledCallTransfer = false;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(CallSettingForDoubleSIMActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(v -> mSwipeBackHelper.backward());

        requestCallPermission();

        // 初始化外呼SIM卡
        simIndex = SPUtils.getInstance().getInt(Constants.SP_CALL_SIM_INDEX, 0);
        binding.tvTelText.setText(CALL_SIM_LIST[simIndex]);

        // 初始化SIM卡1号码
        String sim1 = SPUtils.getInstance().getString(Constants.SP_SIM1_PHONE_NUMBER, "");
        binding.tvSim1Number.setText(sim1);
        // 初始化SIM卡2号码
        String sim2 = SPUtils.getInstance().getString(Constants.SP_SIM2_PHONE_NUMBER, "");
        binding.tvSim2Number.setText(sim2);

        // 初始化呼叫转移设置超时
        int timeout = SPUtils.getInstance().getInt(Constants.SP_CALL_TRANSFER_TIME_OUT, Constants.CALL_TRANSFER_DEFAULT_TIME_OUT);
        binding.tvDeviceInfoText.setText(timeout + "秒");

        // 初始化呼叫转移状态
        enabledCallTransfer = SPUtils.getInstance().getBoolean(Constants.SP_ENABLED_CALL_TRANSFER, false);
        binding.commitCancel.setText(enabledCallTransfer ? "关闭防封设置" : "开启防封设置");
        binding.commitCancel.setBackgroundResource(enabledCallTransfer ? R.drawable.normal_call_btn_close_shape : R.drawable.normal_call_btn_open_shape);
        binding.commitCancel.setEnabled(true);

        // 切换外呼SIM卡
        binding.rlTel.setOnClickListener(v -> {
            List<String> simList = new ArrayList<>();
            simList.add("SIM卡1");
            simList.add("SIM卡2");
            new BottomSheetDialog().initSimpleBottomsheet("选择SIM卡",
                    false,
                    CallSettingForDoubleSIMActivity.this,
                    simList,
                    0,
                    new BottomSheetDialog.OnBottomSheetListener() {
                        @Override
                        public void onSelected(int position) {
                            if(enabledCallTransfer) {
                                enabledCallTransfer = false;
                                SPUtils.getInstance().put(Constants.SP_ENABLED_CALL_TRANSFER, enabledCallTransfer);
                                binding.commitCancel.setText("开启防封设置");
                                binding.commitCancel.setBackgroundResource(R.drawable.normal_call_btn_open_shape);
                            }
                            CallTransferUtil.close(CallSettingForDoubleSIMActivity.this);
                            simIndex = position;
                            SPUtils.getInstance().put(Constants.SP_CALL_SIM_INDEX, position);
                            binding.tvTelText.setText(CALL_SIM_LIST[position]);
                        }

                        @Override
                        public void onCancel() {

                        }

                        @Override
                        public void onDismiss() {

                        }
                    }
            ).show();
        });

        // 设置卡1手机号码
        binding.rlSim1Number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogSettingSim dialogSettingSim = new DialogSettingSim(CallSettingForDoubleSIMActivity.this, 1){
                    @Override
                    public void dealContent(Dialog dialog, String one, int type) {
                        super.dealContent(dialog, one, type);
                        binding.tvSim1Number.setText(one);
                        SPUtils.getInstance().put(Constants.SP_SIM1_PHONE_NUMBER, one);
                    }
                };
                dialogSettingSim.show();
            }
        });

        // 设置卡2手机号码
        binding.rlSim2Number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogSettingSim dialogSettingSim = new DialogSettingSim(CallSettingForDoubleSIMActivity.this, 2){
                    @Override
                    public void dealContent(Dialog dialog, String one, int type) {
                        super.dealContent(dialog, one, type);
                        binding.tvSim2Number.setText(one);
                        SPUtils.getInstance().put(Constants.SP_SIM2_PHONE_NUMBER, one);
                    }
                };
                dialogSettingSim.show();
            }
        });

        // 呼叫转移设置超时
        binding.rlDevice.setOnClickListener(v ->
                new DialogCallTimeOut(CallSettingForDoubleSIMActivity.this)
                        .setOnCallTimeOutConfirmListener(time -> {
                            SPUtils.getInstance().put(Constants.SP_CALL_TRANSFER_TIME_OUT, time);
                            binding.tvDeviceInfoText.setText(time + "秒");
                        }).show()
        );

        // 呼叫转移是否开启的状态按钮
        binding.commitCancel.setOnClickListener(v -> {
            binding.commitCancel.setEnabled(false);
            enabledCallTransfer = !enabledCallTransfer;
            SPUtils.getInstance().put(Constants.SP_ENABLED_CALL_TRANSFER, enabledCallTransfer);
            if(!enabledCallTransfer) {
                CallTransferUtil.close(CallSettingForDoubleSIMActivity.this);
            } else {
                String sim1Number = SPUtils.getInstance().getString(Constants.SP_SIM1_PHONE_NUMBER, "");
                if(TextUtils.isEmpty(sim1Number)) {
                    ToastUtils.showShort("请填写SIM卡1号码");
                    binding.commitCancel.setEnabled(true);
                    return;
                }

                String sim2Number = SPUtils.getInstance().getString(Constants.SP_SIM2_PHONE_NUMBER, "");
                if(TextUtils.isEmpty(sim2Number)) {
                    ToastUtils.showShort("请填写SIM卡2号码");
                    binding.commitCancel.setEnabled(true);
                    return;
                }

                SPUtils.getInstance().put(Constants.SP_SIM_PHONE_NUMBER, simIndex == 1 ? sim1Number : sim2Number);
                SPUtils.getInstance().put(Constants.SP_CALL_TRANSFER_TYPE, 3);
            }
            ToastUtils.showShort("防封设置已" + (enabledCallTransfer ? "开启" : "关闭"));
            binding.commitCancel.setText(enabledCallTransfer ? "关闭防封设置" : "开启防封设置");
            binding.commitCancel.setBackgroundResource(enabledCallTransfer ? R.drawable.normal_call_btn_close_shape : R.drawable.normal_call_btn_open_shape);
            binding.commitCancel.setEnabled(true);
        });

        binding.rlHelp.setOnClickListener(v -> mSwipeBackHelper.forward(HelpActivity.class));
    }

    // 打电话权限申请码
    private static final int CALL_PERMISSION_REQUEST_CODE = 810;

    // 申请打电话权限
    private void requestCallPermission() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(CallSettingForDoubleSIMActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(CallSettingForDoubleSIMActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PERMISSION_REQUEST_CODE);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(requestCode == CALL_PERMISSION_REQUEST_CODE) {
            if(grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                ToastUtils.showShort("请给用户授权");
                mSwipeBackHelper.backward();
            }
        }
    }


}
